package com.coding.service.impl;

import com.coding.base.pojo.BaseModel;
import com.coding.base.pojo.PageQueryModel;
import com.coding.dao.*;
import com.coding.pojo.*;
import com.coding.service.CommentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @auther wangchenghao
 * @date 2020/9/19
 **/
@Service
public class CommentImpl implements CommentService {
    @Autowired
    CommentDao commentDao;
    @Autowired
    PostDao postDao;
    @Autowired
    GroupDao groupDao;
    @Autowired
    TheclassDao theclassDao;
    @Autowired
    MainPlaceDao mainPlaceDao;
    @Autowired
    PlaceDao placeDao;
    @Autowired
    UserDao userDao;

    @Override
    public void getByPostId(int post_id, PageQueryModel pageQueryModel, BaseModel baseModel) throws Exception {
        Post post = postDao.selectByPrimaryKey(post_id);
        if (post == null) {
            baseModel.setFailure("没有想到相关帖子");
        }
        List<Comment> commentList = commentDao.selectByPostId(post_id);
        for (Comment comment:commentList
             ) {
            User user = userDao.selectById(comment.getComment_user_id());
            if (user!=null){
                comment.setUser(user);
            }
        }
        PageInfo pageInfo = new PageInfo(commentList);
        commentList = pageInfo.getList();
        if (commentList.size() == 0) {
            if (pageInfo.getPageNum() > 1) {
                baseModel.setFailure("已经没有下一页了");
            } else {
                baseModel.setOther(2, "没有符合条件的评论");
            }
            return;
        }
        baseModel.setSucceed("查询成功，符合条件的评论共" + commentList.size() + "条", commentList);
    }

    @Override
    public void getByGroupId(int group_id, PageQueryModel pageQueryModel, BaseModel baseModel) throws Exception {
        Group group = groupDao.queryById(group_id);
        if (group == null) {
            baseModel.setFailure("没有查到相关圈子");
        }
        Comment comment = new Comment();
        comment.setSource_id(group_id);
        comment.setComment_kind("group");
        List<Comment> commentList = commentDao.queryAll(comment);
        for (Comment comment2:commentList
        ) {
            User user = userDao.selectById(comment2.getComment_user_id());
            if (user!=null){
                comment2.setUser(user);
            }
        }
        PageInfo pageInfo = new PageInfo(commentList);
        commentList = pageInfo.getList();
        if (commentList.size() == 0) {
            if (pageInfo.getPageNum() > 1) {
                baseModel.setFailure("已经没有下一页了");
            } else {
                baseModel.setOther(2, "没有符合条件的评论");
            }
            return;
        }
        baseModel.setSucceed("查询成功，符合条件的评论共" + commentList.size() + "条", commentList);
    }

    @Override
    public void getByClassId(int class_id, PageQueryModel pageQueryModel, BaseModel baseModel) throws Exception {
        Theclass theclass = theclassDao.queryById(class_id);
        if (theclass == null) {
            baseModel.setFailure("没有查到相关班级");
        }
        Comment comment = new Comment();
        comment.setSource_id(class_id);
        comment.setComment_kind("class");
        List<Comment> commentList = commentDao.queryAll(comment);
        for (Comment comment2:commentList
        ) {
            User user = userDao.selectById(comment2.getComment_user_id());
            if (user!=null){
                comment2.setUser(user);
            }
        }
        PageInfo pageInfo = new PageInfo(commentList);
        commentList = pageInfo.getList();
        if (commentList.size() == 0) {
            if (pageInfo.getPageNum() > 1) {
                baseModel.setFailure("已经没有下一页了");
            } else {
                baseModel.setOther(2, "没有符合条件的评论");
            }
            return;
        }
        baseModel.setSucceed("查询成功，符合条件的评论共" + commentList.size() + "条", commentList);
    }

    @Override
    public void getByMainPlaceId(int main_place_id, PageQueryModel pageQueryModel, BaseModel baseModel) throws Exception {
        MainPlace mainPlace = mainPlaceDao.selectByPrimaryKey(main_place_id);
        if (mainPlace == null) {
            baseModel.setFailure("没有查找到相关景点");
        }
        Comment comment = new Comment();
        comment.setSource_id(main_place_id);
        comment.setComment_kind("main_place");
        List<Comment> commentList = commentDao.queryAll(comment);
        for (Comment comment2:commentList
        ) {
            User user = userDao.selectById(comment2.getComment_user_id());
            if (user!=null){
                comment2.setUser(user);
            }
        }
        PageInfo pageInfo = new PageInfo(commentList);
        commentList = pageInfo.getList();
        if (commentList.size() == 0) {
            if (pageInfo.getPageNum() > 1) {
                baseModel.setFailure("已经没有下一页了");
            } else {
                baseModel.setOther(2, "没有符合条件的评论");
            }
            return;
        }
        baseModel.setSucceed("查询成功，符合条件的评论共" + commentList.size() + "条", commentList);
    }

    @Override
    public void getByPlaceId(int place_id, PageQueryModel pageQueryModel, BaseModel baseModel) throws Exception {
        Place place = placeDao.selectByPrimaryKey(place_id);
        Comment comment = new Comment();
        comment.setSource_id(place_id);
        comment.setComment_kind("place");
        List<Comment> commentList = commentDao.queryAll(comment);
        for (Comment comment2:commentList
        ) {
            User user = userDao.selectById(comment2.getComment_user_id());
            if (user!=null){
                comment2.setUser(user);
            }
        }
        PageInfo pageInfo = new PageInfo(commentList);
        commentList = pageInfo.getList();
        if (commentList.size() == 0) {
            if (pageInfo.getPageNum() > 1) {
                baseModel.setFailure("已经没有下一页了");
            } else {
                baseModel.setOther(2, "没有符合条件的评论");
            }
            return;
        }
        baseModel.setSucceed("查询成功，符合条件的评论共" + commentList.size() + "条", commentList);
    }

    @Override
    public void getByPostIdTime(int post_id, PageQueryModel pageQueryModel, BaseModel baseModel) throws Exception {

    }

    @Override
    public void addNewPost(Comment comment, BaseModel baseModel) throws Exception {
        User user = userDao.selectById(comment.getComment_user_id());
        if (user!=null){
            comment.setComment_picture_id(user.getUser_image());
        }
        if (commentDao.insertSelective(comment) == 1) {
            Comment newComment = commentDao.selectByPrimaryKey(comment.getComment_id());
            if (newComment != null) {
                baseModel.setSucceed("新增评论成功并返回", newComment);
                return;
            }

            baseModel.setSucceed("新增评论成功", comment);
        } else {
            baseModel.setFailure("新增评论失败");
        }
    }
}
